#ifndef PLAY_H
#define PLAY_H

#include <cmath>

#include <raylib.h>
#include <cmath>

#include <raymath.h>

#include <cmath>

#include <raylib.h>
#include <cmath>

#include <raymath.h>

#include "Geography.h"
#include "utilities.h"

void Geography::init() {
	Vector3 yellowRoadNormal = {0, 0, 1}; // 黄道平面（地球公转轨道面）法向量（世界坐标系Z轴）
	float yellowAndRedAngle = 23.26; // 黄赤夹角
	float earthAxesAngle = 90 - yellowAndRedAngle; // 地轴与黄道平面的夹角
	float earthAxesAngleRad = DEG2RAD * earthAxesAngle;
	m_redRoadZNormal = {cosf(earthAxesAngleRad), 0, sinf(earthAxesAngleRad)}; // 赤道平面法向量（地轴，地球Z轴基向量）
	// 地球X轴基向量, 地球Y轴基向量
	auto [redRoadXNormal, redRoadYNormal] = getOtherTwoAxis(m_redRoadZNormal);
	m_redRoadXNormal = redRoadXNormal;
	m_redRoadYNormal = redRoadYNormal;
}

Vector3 newAxis(Vector3 axisOne, Vector3 axisTwo, float alphaRad) {
	Vector3 newAxis;
	newAxis.x = axisOne.x * cosf(alphaRad) + axisTwo.x * sinf(alphaRad);
	newAxis.y = axisOne.y * cosf(alphaRad) + axisTwo.y * sinf(alphaRad);
	newAxis.z = axisOne.z * cosf(alphaRad) + axisTwo.z * sinf(alphaRad);
	return newAxis;
}

void Geography::update(std::chrono::nanoseconds delt) {
	// 根据自转速度更新地球系坐标轴
	float delT_update = delt.count()*1e-6;
	float earthRotationAngleSpeed = 7.292*1e-5;
	float earthRotationAngleRad = earthRotationAngleSpeed * delT_update;
	m_redRoadXNormal = newAxis(m_redRoadXNormal, m_redRoadYNormal, earthRotationAngleRad);
	m_redRoadYNormal = Vector3CrossProduct(m_redRoadZNormal, m_redRoadXNormal);
	// 根据公转速度更新地球位置
	float earthRotationInSunAngleSpeed = DEG2RAD * 1.141552511415525e-5;
	m_earthAngleRotation += (earthRotationInSunAngleSpeed*delT_update);
	m_earthPos = {m_distrance * cosf(m_earthAngleRotation), m_distrance * sinf(m_earthAngleRotation), 0};
}

void Geography::draw() {
	// 世界坐标系，以太阳为原点
	float earthSomeCircleRadius = m_earthEquatorialRadius + 0.0001;
	float sunAxisRatio = 160;
	Vector3 sunAxisXSubPos = Vector3Subtract(m_sunPos, {sunAxisRatio, 0, 0});
	Vector3 sunAxisYSubPos = Vector3Subtract(m_sunPos, {0, sunAxisRatio, 0});
	Vector3 sunAxisZSubPos = Vector3Subtract(m_sunPos, {0, 0, sunAxisRatio});
	DrawLine3D(sunAxisXSubPos, {sunAxisRatio, 0, 0}, RED);
	DrawLine3D(sunAxisYSubPos, {0, sunAxisRatio, 0}, GREEN);
	DrawLine3D(sunAxisZSubPos, {0, 0, sunAxisRatio}, BLUE);
	// 画太阳
	DrawSphere(m_sunPos, m_sunRadius, RED);
	// 画地球
	DrawSphere(m_earthPos, m_earthEquatorialRadius, BLUE);
	// 太阳到地球的连线
	DrawLine3D(m_sunPos, m_earthPos, WHITE);
	
	Vector3 yellowRoadNormal = {0, 0, 1}; // 黄道平面（地球公转轨道面）法向量（世界坐标系Z轴）
	float yellowAndRedAngle = 23.26; // 黄赤夹角
	float earthAxesAngle = 90 - yellowAndRedAngle; // 地轴与黄道平面的夹角
	
	// 两个坐标系坐标的转换，先基变换后平移（先按原点相同变换坐标基，再平移到对应的原点位置）
	// 地球坐标轴基向量
	Matrix modelCoordinateAxisBase = {
		m_redRoadXNormal.x, m_redRoadYNormal.x, m_redRoadZNormal.x, 0,
		m_redRoadXNormal.y, m_redRoadYNormal.y, m_redRoadZNormal.y, 0,
		m_redRoadXNormal.z, m_redRoadYNormal.z, m_redRoadZNormal.z, 0,
		0, 0, 0, 1
	};
	// 世界坐标轴基向量
	Matrix worldCoordinateAxisBase = {
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
	};
	// 世界系到地球系基变换后平移矩阵
	Matrix worldToModelTransfom = {
		1, 0, 0, m_sunPos.x - m_earthPos.x,
		0, 1, 0, m_sunPos.y - m_earthPos.y,
		0, 0, 1, m_sunPos.z - m_earthPos.z,
		0, 0, 0, 1
	};
	// 地球系到世界系基变换后平移矩阵
	Matrix modelToWorldTransfom = {
		1, 0, 0, m_sunPos.x + m_earthPos.x,
		0, 1, 0, m_sunPos.y + m_earthPos.y,
		0, 0, 1, m_sunPos.z + m_earthPos.z,
		0, 0, 0, 1
	};
	// 世界基到地球基过渡矩阵（变换矩阵）
	Matrix worldAxisBaseToModel = MatrixMultiply(MatrixInvert(worldCoordinateAxisBase), modelCoordinateAxisBase);
	// 地球基到世界基过渡矩阵（变换矩阵）
	Matrix modleAxisBaseToWorld = MatrixInvert(worldAxisBaseToModel);
	// 世界系坐标到地球系坐标变换矩阵
	Matrix worldAxisPosToModel = MatrixMultiply(modleAxisBaseToWorld, worldToModelTransfom);
	// 地球系坐标到太阳系坐标变换矩阵
	Matrix modelAxisPosToWorld = MatrixMultiply(worldAxisBaseToModel, modelToWorldTransfom);
	
	// 地球系坐标轴
	DrawLine3D(Vector3Transform({-1, 0, 0}, modelAxisPosToWorld), Vector3Transform({1, 0, 0}, modelAxisPosToWorld), RED);
	DrawLine3D(Vector3Transform({0, -1, 0}, modelAxisPosToWorld), Vector3Transform({0, 1, 0}, modelAxisPosToWorld), GREEN);
	DrawLine3D(Vector3Transform({0, 0, -1}, modelAxisPosToWorld), Vector3Transform({0, 0, 1}, modelAxisPosToWorld), BLUE);
	
	// 画赤道
	Vector3 redRoadAtWorldRotationAxis = Vector3CrossProduct(yellowRoadNormal, m_redRoadZNormal);
	DrawCircle3D(m_earthPos, earthSomeCircleRadius+0.001, redRoadAtWorldRotationAxis, yellowAndRedAngle, RED);
	
	// 画光照面
	Vector3 sunLightDirectToEarthNormal = Vector3Normalize(Vector3Subtract(m_earthPos, m_sunPos));
	float lightSurfaceAtWorldRotationAngle = RAD2DEG * Vector3Angle(sunLightDirectToEarthNormal, yellowRoadNormal);
	Vector3 lightSurfaceAtWorldRotationAxis = Vector3CrossProduct(sunLightDirectToEarthNormal, yellowRoadNormal);
	DrawCircle3D(m_earthPos, earthSomeCircleRadius, lightSurfaceAtWorldRotationAxis, lightSurfaceAtWorldRotationAngle, BLACK);
	
	uint16_t sunLightRayCount = 50;
	std::vector<Vector3> lightSurfaceCirclePoints = getCirclePointsPos(m_earthPos, sunLightDirectToEarthNormal, earthSomeCircleRadius, sunLightRayCount);
	for (Vector3& pointPos : lightSurfaceCirclePoints) {
		DrawLine3D(pointPos, m_sunPos, WHITE);
	}
	
	Vector3 sunLightDirectToEarthPointPos = Vector3Add(m_earthPos, Vector3Scale(Vector3Negate(sunLightDirectToEarthNormal), earthSomeCircleRadius-0.001));
	DrawSphere(sunLightDirectToEarthPointPos, 0.0006, RED);
	
	// 画经线
	uint8_t meridianCount = 20;
	std::vector<Vector3> meridianAtRedRoadPointPos = getCirclePointsPos(m_earthPos, m_redRoadXNormal, m_redRoadYNormal, earthSomeCircleRadius, meridianCount);
	for (Vector3& pos : meridianAtRedRoadPointPos) {
		//		DrawCircle3D(m_earthPos, earthSomeCircleRadius-0.001, normal, 90, GREEN);
		Vector3 meridianSurfaceNormal = Vector3CrossProduct(Vector3Subtract(pos, m_earthPos), m_redRoadZNormal);
		float meridianSurfaceAtWorldRotationAngle = RAD2DEG * Vector3Angle(meridianSurfaceNormal, yellowRoadNormal);
		Vector3 meridianSurfaceAtWorldRotationAxis = Vector3CrossProduct(meridianSurfaceNormal, yellowRoadNormal);
		DrawCircle3D(m_earthPos, earthSomeCircleRadius, meridianSurfaceAtWorldRotationAxis, -meridianSurfaceAtWorldRotationAngle, GREEN);
		DrawLine3D(m_earthPos, pos, GREEN);
	}
	
	// 画纬线
	uint8_t parallelCount = 20;
	float deltParallelAngle = 180/parallelCount;
	float parallelAngle = -90 + deltParallelAngle;
	for (int i = 0;i<parallelCount-1;++i) {
		//		DrawCircle3D(m_earthPos, earthSomeCircleRadius-0.001, normal, 90, GREEN);
		float parallelAngleDeg = DEG2RAD * parallelAngle;
		float height = earthSomeCircleRadius*sinf(parallelAngleDeg);
		float parallelRadius = earthSomeCircleRadius*cosf(parallelAngleDeg);
		Vector3 modelHeight = Vector3Scale(m_redRoadZNormal, height);
		DrawCircle3D(Vector3Add(m_earthPos, modelHeight), parallelRadius, {0,1,0}, yellowAndRedAngle, GOLD);
		parallelAngle += deltParallelAngle;
	}
	//	DrawCircle3D(m_earthPos, m_earthPoleRadius + 0.0001, yellowRoadNormal, 0, YELLOW);
	//	DrawCircle3D(m_earthPos, m_earthPoleRadius + 0.0001, {0, 1, 0}, yellowAndRedAngle, RED);
}






#endif
